package com.ayu.shortlink.project.mq.producer;

import cn.hutool.core.util.StrUtil;
import com.ayu.shortlink.project.common.constant.RocketMQConstant;
import com.ayu.shortlink.project.mq.basics.base.BaseSendMessageExtendDTO;
import com.ayu.shortlink.project.mq.basics.base.MessageWrapper;
import com.ayu.shortlink.project.mq.basics.event.HotKeySaveCaffeineEvent;
import org.apache.rocketmq.common.message.MessageConst;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.messaging.Message;
import org.springframework.messaging.support.MessageBuilder;
import org.springframework.stereotype.Component;

import java.util.UUID;

/**
 * hotkey保存到Caffeine的生产者
 */
@Component
public class HotKeySaveToCaffeineProducer extends AbstractCommonSendProduceTemplate<HotKeySaveCaffeineEvent>{
    public HotKeySaveToCaffeineProducer(@Autowired RocketMQTemplate rocketMQTemplate) {
        super(rocketMQTemplate);
    }

    @Override
    protected BaseSendMessageExtendDTO buildBaseSendMessageExtendDTO(HotKeySaveCaffeineEvent messageSendEvent) {
        String keys = StrUtil.isBlank(messageSendEvent.getKeys())?
                UUID.randomUUID().toString():messageSendEvent.getKeys();
        return BaseSendMessageExtendDTO.builder()
                .topic(RocketMQConstant.CACHE_HOTKEY_TOPIC)
                .keys(keys)
                .eventName("通知Caffeine缓存HotKey")
                .sentTimeout(2000L)
                .build();
    }

    @Override
    protected Message<?> buildMessage(HotKeySaveCaffeineEvent messageSendEvent, BaseSendMessageExtendDTO requestParam) {
        String keys = StrUtil.isBlank(messageSendEvent.getKeys())?
                UUID.randomUUID().toString():messageSendEvent.getKeys();
        return MessageBuilder
                .withPayload(new MessageWrapper(keys,messageSendEvent))
                .setHeader(MessageConst.PROPERTY_KEYS, keys)
                .setHeader(MessageConst.PROPERTY_TAGS, requestParam.getTag())
                .build();
    }
}
